package com.eims.ball.info.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.MySingleton;
import com.android.volley.toolbox.StringXmlRequest;
import com.contans.view.PeilvOupeiVo;
import com.eims.ball.info.adapter.PeilvOupeiListAdapter;
import com.eims.sls_app.view.MyToast;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import bls.juejin.cn.R;
import progressBar.ProgressDialogBar;

public class PeilvOupeiFragment extends Fragment {
	private Activity activity;
	private  ListView listviewOupei;
	private PeilvOupeiListAdapter adapter;

	private String infoId, hid, gid, mainName, GuesName;

	private MyHandler handler;

	
	private ArrayList<PeilvOupeiVo> peilvOupeiVos;
	
	
	



	public PeilvOupeiFragment() {
		super();
	}

	public static PeilvOupeiFragment newInstance(String infoId, String hid, String gid,String mainName,String GuesName) {

		Bundle args = new Bundle();

		PeilvOupeiFragment fragment = new PeilvOupeiFragment();
		args.putString("infoId",infoId);
		args.putString("hid",hid);
		args.putString("gid",gid);
		args.putString("mainName",mainName);
		args.putString("GuesName",GuesName);
		fragment.setArguments(args);
		return fragment;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.peilv_oupei_info, container, false);
		activity = getActivity();
		Bundle args = getArguments();
		if (args != null) {
			this.infoId = args.getString("infoId");
			this.hid= args.getString("hid");
			this.gid = args.getString("gid");
			this.mainName= args.getString("mainName");
			this.GuesName= args.getString("GuesName");

		}
		handler = new MyHandler();

		init(view);
		getBalllInfo(infoId);
		return view;
	}

	private void init(View view) {

		listviewOupei=(ListView) view.findViewById(R.id.listview_oupei);
	

	}

	public void getBalllInfo(String id) {
		MySingleton mySingleton = MySingleton.getInstance(activity);
		ProgressDialogBar.startProgress(activity, "", false);
		StringXmlRequest jsonObjectRequest = new StringXmlRequest(Request.Method.GET,
				"http://01link.igowu123.com/qtjsbf/jc/odds/ou/" + id + ".xml", new Response.Listener<String>() {
					@Override
					public void onResponse(String tn) {

//						Log.i("RealTime", "request--------success------" + tn);
						ProgressDialogBar.stopProgress();
						try {
							JSONObject item = XML.toJSONObject(tn);

//							Log.e("JSON", item.toString());

							

							String resultRep = item.optString("rows");

							item = new JSONObject(resultRep);

						

							String CreditinfoStr = item.getString("row");

							if (CreditinfoStr != null && !"".equals(CreditinfoStr)) {
								JSONArray array_jifen = new JSONArray(CreditinfoStr);

								peilvOupeiVos=new ArrayList<PeilvOupeiVo>();
								PeilvOupeiVo peilvOupeiVo;

								for (int i = 0; i < array_jifen.length(); i++) {
									peilvOupeiVo = new PeilvOupeiVo();

									JSONObject array2 = array_jifen.getJSONObject(i);
									peilvOupeiVo.setCompany(array2.optString("company"));
									peilvOupeiVo.setCpyid(array2.optString("cpyid"));
									peilvOupeiVo.setOld__p1(array2.optString("old__p1"));
									peilvOupeiVo.setOld__p2(array2.optString("old__p2"));
									peilvOupeiVo.setOld__p3(array2.optString("old__p3"));
									peilvOupeiVo.setNew__p1(array2.optString("new__p1"));
									peilvOupeiVo.setNew__p2(array2.optString("new__p2"));
									peilvOupeiVo.setNew__p3(array2.optString("new__p3"));
									peilvOupeiVo.setReturn__rate(array2.optString("return__rate"));
									peilvOupeiVo.setOld_return_rate(array2.optString("old_return_rate"));
									peilvOupeiVo.setTimestr(array2.optString("timestr"));
									peilvOupeiVos.add(peilvOupeiVo);

								}
								

							}

						
						
							ProgressDialogBar.stopProgress();
							handler.sendEmptyMessage(1);

							//
						} catch (JSONException e) {
							Log.e("JSON", "xml->json失败" + e.getLocalizedMessage());
//							MyToast.getToast(activity, "获取失败").show();

						}
						ProgressDialogBar.stopProgress();
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Log.i("RealTime", "request--------success------" + error);
						MyToast.getToast(activity, "获取失败").show();
						ProgressDialogBar.stopProgress();
					}

				}) {

			@Override
			protected Map<String, String> getParams() throws AuthFailureError {

				Map<String, String> params = new HashMap<String, String>();
				// params.put("appid", "0af5281058488ec1784e433f69d45d81");
				// params.put("custNo", "gl00014156");
				// params.put("orderId", attach);

				return params;
			}
		};

		jsonObjectRequest.setShouldCache(false);// 是否设置缓存

		mySingleton.addToRequestQueue(jsonObjectRequest);

	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			ProgressDialogBar.stopProgress();
			switch (msg.what) {
			case 0:
				ProgressDialogBar.stopProgress();
				break;
			case 1:
				ProgressDialogBar.stopProgress();
				adapter=new PeilvOupeiListAdapter(activity,peilvOupeiVos);
				listviewOupei.setAdapter(adapter);
				
				
				break;
			default:
				ProgressDialogBar.stopProgress();
				Toast.makeText(getActivity(), "网络异常，加载失败。", Toast.LENGTH_SHORT).show();
				break;
			}
			super.handleMessage(msg);
		}
	}

}
